package com.ferry.id.pool.dao;

import com.ferry.id.pool.domain.Alloc;
import org.apache.ibatis.annotations.*;

import java.util.List;
import java.util.Map;

/**
 * Created by daiyong
 */
public interface AllocMapper {

	@Select("SELECT namespace, max_id, step, update_time FROM ferry_id_alloc")
	@Results(value = {
			@Result(column = "namespace", property = "namespace"),
			@Result(column = "max_id", property = "maxId"),
			@Result(column = "step", property = "step"),
			@Result(column = "update_time", property = "updateTime")
	})
	List<Alloc> getAllAllocs();

	@Select("SELECT namespace, max_id, step FROM ferry_id_alloc WHERE namespace = #{namespace}")
	@Results(value = {
			@Result(column = "namespace", property = "namespace"),
			@Result(column = "max_id", property = "maxId"),
			@Result(column = "step", property = "step")
	})
	Alloc getAlloc(@Param("namespace") String namespace);

	@Update("UPDATE ferry_id_alloc SET max_id = max_id + step, update_time = #{updateTime} WHERE namespace = #{namespace}")
	void updateMaxId(Map<String, Object> params);

	@Update("UPDATE ferry_id_alloc SET max_id = max_id + #{step}, update_time = #{updateTime} WHERE namespace = #{namespace}")
	void updateMaxIdByCustomStep(@Param("alloc") Alloc alloc);

	@Select("SELECT namespace FROM ferry_id_alloc")
	List<String> getAllNamespaces();

}
